<?php
class AddressAction extends MemberAction {
	
	
	public function index(){
		$this->list = M('Address') -> where(array('member_id' => $this -> memberInfo['id'])) -> select();
		$this->display();
	}
	public function add(){
		
		$this->display();
	}
	
	
	
	public function insert(){
//		处理添加地址
		if(IS_POST){
			$yourname = $this->_post('yourname');
			$area = $this->_POST('area');
			$tel = $this->_post('tel');
			$address = $this->_post('address');
			$shen = getRegionById($area[0]);
			$shi = getRegionById($area[1]);
			$qu =getRegionById($area[2]);
			$address = $shen[0]['region_name'].$shi[0]['region_name'].$qu[0]['region_name'].$address;
			$regionids = ','.implode(',', $area).',';
//			保存地址
			$this->saveAddress($yourname,$tel,$address, $regionids);
			$this->success("保存成功",U('index'));

		}
		
	}
	
	
	public function saveAddress($yourname="",$tel="",$address="", $area = ''){
		$yourname = empty($yourname) ? $_POST['yourname'] : $yourname;
		$tel = empty($tel) ? $_POST['tel'] : $tel;
		$address = empty($address) ? $_POST['address'] : $address;

		if(empty($yourname) || empty($tel) || empty($address)){
			return false;
		}else{
			$data['yourname'] = $yourname;
			$data['tel'] =$tel;
			$data['address'] = $address;
			$data['member_id'] = $this -> memberInfo['id'];
			$data['regionids'] = $area;
			$data['site_id'] = __SITEID__;
		    $data[C('TOKEN_ON')] = $_POST[C('TOKEN_ON')];
			$Address = M('address');
			
			
			
			
			
			
			$address = $Address->where($data)->find();
			// 确保地址没被重复保存
			if(empty($address)){
				$Address->add($data);
				return true;
			}else{
				return false;
			}
		}
	}
	public function delete(){
	$id = intval($this -> _get('id'));
	$map['id']=$id;
	$map['member_id']=$this->memberInfo['id'];
	$res=M('Address')->where($map)->delete();
	
	if($res){
	$this->success('删除成功');	
	}else{
	$this->error('删除失败');		
	}
	
	
	}
	//修改地址
	public function edit(){
	  $id = intval($this -> _get('id'));
	  if($id > 0){
		  $info = M('Address') -> where(array('id' => $id, 'member_id' => $this->memberInfo['id'])) -> find();
		  if(!$info){
			 $this->error('参数错误！'); 
		  }
		  
		  
		  $area = trim($info['regionids'], ',');
		  
		  
		  $address = getRegionById($area);
		  foreach($address as $k => $v){
			  $addresstext[] = $v['region_name'];
		  }
		  
		  $info['address'] = str_replace(implode('',$addresstext), '', $info['address']);
		  
		  $this->assign('info', $info);
		  $this->display();	
	  }else{
		  if(IS_POST){
			  $id = intval($this -> _post('id'));
			  if($id > 0){
				  $map['id'] = $id;
				  $map['member_id'] = $this->memberInfo['id'];
				  $area = $this->_POST('area');
				  $_POST['regionids'] = ','.implode(',',$_POST['area']).',';
				  $addresstext = array();
				  $address = getRegionById($area);
				  //$_POST['address'] = 
				  foreach($address as $k => $v){
					  $addresstext[] = $v['region_name'];
				  }
				 // dump($_POST['regionids']);
				  $_POST['address'] = implode('',$addresstext).$_POST['address'];
				  
				  $ret = M('Address') -> where($map) -> data($_POST) -> save();
				  if($ret !== false){
					  $this->success('保存成功！', U('index'));
				  }else{
					  $this->error(M('Address') -> getError()); 
				  }
			  }else{
				 $this->error('参数错误'); 
			  }
		  }else{
			 $this->error('参数错误'); 
		  }
	  }
	  
	}
}

